//
//  MainViewController.swift
//  WeiBoSwift
//
//  Created by lx on 16/9/19.
//  Copyright © 2016年 Qiujunhang. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
      
        configureChildViewController()

        
        
    }
    
    
    override func viewWillAppear(animated: Bool) {
        
        super.viewWillAppear(animated)

        configureAddBtn()
        
    }
    
    // MARK: - 加载子控制器的私有方法
    private func configureChildViewController(){
       
        
        
        
        
        
        
        
        addChildViewController("HomeViewController", title: "主页", imageName: "tabbar_home")
        addChildViewController("MessageViewController", title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController("NullViewController", title: "", imageName: "")
        
        addChildViewController("DiscoverViewController", title: "发现", imageName: "tabbar_discover")
        addChildViewController("PersonalViewController", title: "我", imageName: "tabbar_profile")
        
    

    }
    
    
    private func addChildViewController(childControllerName: String, title : String, imageName : String) {
        
        let nameSpace : String = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String

        let cla : AnyClass? = NSClassFromString(nameSpace + "." + childControllerName)

        let classVC = cla as! UIViewController.Type
        
        let vc : UIViewController = classVC.init()
        
        
        vc.tabBarItem.title = title
        vc.tabBarItem.image = UIImage.init(named: imageName)
        vc.tabBarItem.selectedImage = UIImage.init(named: imageName +  "_highlighted")
        
        let navigVC = UINavigationController()
        navigVC.addChildViewController(vc)
        
        self.addChildViewController(navigVC)
        
    
    }

    
    // MARK: - 配置button的 frame
    private func configureAddBtn(){

        tabBar.addSubview(addBtn)
        

        let width = UIScreen.mainScreen().bounds.size.width/CGFloat(viewControllers!.count)
        
        let height  : CGFloat = 44.0

        addBtn.frame = CGRectOffset(CGRectMake(0, 0, width, height), 2 * width, 2.5)

    }
    
    // MARK: - 懒加载创建按钮
    private lazy var addBtn : UIButton = {
        
        let btn = UIButton()

        btn.setImage(UIImage.init(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage.init(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)

        btn.setBackgroundImage(UIImage.init(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage.init(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)

        btn.addTarget(self, action: "addBtnClick", forControlEvents: .TouchUpInside)
        
        
        return btn
    }()
    
   
    //MARK: - 按钮的事件处理
    func addBtnClick(){
        
        
        print("按钮被点击");
        
        
    }
    
    
    
}
